

package com.bei_en.teacher.admin.service.users;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.users.bean.Users;
//import com.bei_en.teacher.admin.model.users.Users;
import com.bei_en.teacher.admin.model.users.condition.UsersCondition;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
//import com.bei_en.teacher.admin.model.users.vo.UsersCondition;
import com.bei_en.teacher.admin.persistence.dao.users.UsersDao;

/**
 * 账户
 * @author Guocg
 * @date 2016-7-26 22:44:04
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class UsersService extends BaseService{

    @Autowired
    private UsersDao usersDao;

    public void insert(UsersVo model) {

    	 usersDao.insert(model.getUsers());
    }

    /**
     * 插入并返回生成的id
     * @param model
     * @return
     */
    public Integer merge(UsersVo model) {

	usersDao.insert(model.getUsers());
	return usersDao.findLastInsertedId();

    }

    public void delete(Integer modelInteger) {

    	usersDao.delete(modelInteger);
    }

    public UsersVo findById(Integer modelInteger) {

    	UsersVo model = new UsersVo(usersDao.findById(modelInteger));
    	return model;
    }

    public void update(UsersVo model) {

    	usersDao.update(model.getUsers());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(UsersVo model) {

    	usersDao.updateSelective(model.getUsers());
    }

    public int countAll() {

    	return usersDao.countAll();
    }

    public List<UsersVo> findAll() {

		List<UsersVo> list = new ArrayList<UsersVo>();
		List<Users> list1 = usersDao.findAll();
		for (Users model : list1) {
		    list.add(new UsersVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return usersDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<UsersVo> findUsersPage(UsersCondition vo) {

		Integer totalCount = searchUsersNum(vo);
		List<UsersVo> list = new ArrayList<UsersVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "user_id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<Users> list1 = usersDao.findPageBreakByCondition(vo, rb);
		    for (Users model : list1) {
		    	list.add(new UsersVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchUsersNum(UsersCondition Vo) {

    	return usersDao.findNumberByCondition(Vo);
    }

}
